package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.page.PageQuery;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.CodeUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TProd;
import com.ruoyi.system.domain.TProdTask;
import com.ruoyi.system.domain.TProdTaskLog;
import com.ruoyi.system.domain.dto.TProdTaskDTO;
import com.ruoyi.system.domain.vo.TProdTaskLogVO;
import com.ruoyi.system.domain.vo.TProdTaskVO;
import com.ruoyi.system.service.ITProdRecordService;
import com.ruoyi.system.service.ITProdService;
import com.ruoyi.system.service.ITProdTaskLogService;
import com.ruoyi.system.service.ITProdTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * @author wux
 * @date 2023/5/26 16:04
 */
@Api(tags = "(客户端) 生产任务接口")
@RestController
@RequestMapping("/client/prod/task")
public class TProdTaskController {
    @Autowired
    private ITProdTaskService prodTaskService;
    @Autowired
    private ITProdRecordService prodRecordService;
    @Autowired
    private ITProdService prodService;

    @PostMapping("/save")
    @ApiOperation(value = "新增生产任务")
    public ResultEntity save(@RequestBody TProdTaskDTO.TProdTaskAddDTO params) {
        TProdTask entity = new TProdTask();
        BeanUtils.copyProperties(params, entity);
        int taskNum = prodTaskService.count(new LambdaQueryWrapper<TProdTask>()
                .eq(TProdTask::getProdType,params.getProdType())
                .like(TProdTask::getTaskCode, "JH-" + DateUtils.parseDateToStr("yyyyMMdd", new Date()))
                .apply("DATE_FORMAT(create_time,'%Y-%m-%d') = CURDATE()"));
        String taskCode = CodeUtils.getProdTaskCode(taskNum);
        entity.setTaskCode(taskCode);
        prodTaskService.saveOrUpdate(entity);
        return ResultEntity.createSuccess();
    }

    @PostMapping("/updateTaskQuantity")
    @ApiOperation(value = "任务产量调整")
    public ResultEntity updateTaskQuantity(TProdTaskLog params){
        prodTaskService.saveTaskLog(params);
        return ResultEntity.createSuccess();
    }

    @GetMapping("/queryTaskChangeLog")
    @ApiOperation(value = "任务产量调整日志查询列表",response = TProdTaskLogVO.class)
    public ResultEntity queryTaskChangeLog(PageQuery page,
                                           @RequestParam(value = "taskId") Long taskId){
        return ResultEntity.createSuccess(prodTaskService.queryTaskChangeLog(page,taskId));
    }

//    @PostMapping("/update")
//    @ApiOperation(value = "编辑生产任务")
//    public ResultEntity update(@RequestBody TProdTaskDTO.TProdTaskUpdateDTO params) {
//        //已有产量的任务不允许修改产品
//        TProd prod = prodService.getById(params.getProdId());
//        TProdTask prodTask = prodTaskService.getById(params.getId());
//        int finishNum = prodRecordService.sumRecord(prod.getProjectId());
//        if(finishNum > 0 && prod.getId() != prodTask.getProdId()){
//            throw new RuntimeException("生产中的任务不允许修改产品!");
//        }
//        TProdTask entity = new TProdTask();
//        BeanUtils.copyProperties(params, entity);
//        prodTaskService.updateById(entity);
//        return ResultEntity.createSuccess();
//    }

    @GetMapping("/list")
    @ApiOperation(value = "生产任务列表查询",response = TProdTaskVO.class)
    public ResultEntity list(TProdTaskDTO params) {
        params.setProjectId(1L);
        return ResultEntity.createSuccess(PageUtil.getPageList(prodTaskService.listByPage(params)));
    }

}
